package hotLeetcode;

import java.util.*;

public class H94Leetcode {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    //中序遍历
    public List<Integer> inorderTraversal1(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        center(root, result);

        return result;
    }


    private void center(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }

        center(root.left,list);
        list.add(root.val);
        center(root.right, list);

    }


    //前序遍历
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        pre(root, result);
        return result;

    }

    public void pre(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        list.add(root.val);
        pre(root.left, list);
        pre(root.right, list);

    }

    //后序遍历
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        back(root, result);

        return result;
    }

    private void back(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        back(root.left, list);
        back(root.right, list);
        list.add(root.val);

    }


    //用栈的方式实现
    public List<Integer> inorderTraversal(TreeNode root) {
        //中序遍历 就是 左子树 --> 根节点 -> 右子树
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> result = new ArrayList<>();
        while (!stack.isEmpty() || root != null) {
            if (root != null) {
                stack.add(root);
                root = root.left;
            } else {
                //说明左边到头了
                TreeNode pop = stack.pop();
                result.add(pop.val);
                root = pop.right;
            }
        }


        return result;

    }


    // 1  5  3  4  6
    public void inOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode curr = root;
        while (curr != null || !stack.isEmpty()) {
            while (curr != null) {
                stack.push(curr);
                curr = curr.left;
            }

            curr = stack.pop();
            System.out.print(curr.val + " ");


            curr = curr.right;
        }


    }


    //中序遍历
    public boolean isValidBST(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        double inorder = -Double.MAX_VALUE;

        while (!stack.isEmpty() ||root != null  ) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }

            root = stack.pop();
            if (root.val <= inorder) {
                return false;
            }



            System.out.print(root.val + " ");
            inorder = root.val;
            root = root.right;


        }

        return true;
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(1);
        root.right = new TreeNode(4);
        root.left.right = new TreeNode(2);
        H94Leetcode h94Leetcode = new H94Leetcode();
        System.out.println(h94Leetcode.inorderTraversal(root));


    }


    }
